import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_web_study/api/app_config.dart';
import 'package:flutter_web_study/router/application.dart';
import 'package:flutter_web_study/utils/sp_utils.dart';
import 'package:flutter_web_study/utils/toast_utils.dart';

import '../api/dio_utils.dart';
import '../api/service_api.dart';
import '../router/routes.dart';
import 'package:logger/logger.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("登录页面"), centerTitle: true),
      body: SizedBox(
        width: double.maxFinite,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(
              width: 300,
              child: TextField(
                inputFormatters: [
                  FilteringTextInputFormatter.allow(RegExp("[a-zA-Z0-9]"))
                ],
                controller: _usernameController,
                decoration: const InputDecoration(
                    labelText: '账号',
                    hintText: '请输入账号',
                    prefixIcon: Icon(Icons.account_circle_rounded)),
                onChanged: (value) {},
              ),
            ),
            SizedBox(
              width: 300,
              child: TextField(
                controller: _passwordController,
                inputFormatters: [
                  // FilteringTextInputFormatter.digitsOnly,
                  FilteringTextInputFormatter.allow(RegExp("[a-zA-Z0-9]"))
                ],
                decoration: const InputDecoration(
                    labelText: '密码',
                    hintText: '请输入密码',
                    prefixIcon: Icon(Icons.lock)),
                // keyboardType: TextInputType.visiblePassword,
                // obscureText: true,
                onChanged: (value) {},
              ),
            ),
            const SizedBox(height: 10),
            MaterialButton(
                color: Colors.blue,
                textColor: Colors.white,
                minWidth: 300,
                height: 50,
                child: const Text(
                  "登录",
                  textAlign: TextAlign.center,
                ),
                onPressed: () {
                  var password = _passwordController.text.toString().trim();
                  var userName = _usernameController.text.toString().trim();
                  if (userName.isEmpty) {
                    ToastUtils.showToast(msg: "请输入账号");
                    return;
                  }
                  if (password.isEmpty) {
                    ToastUtils.showToast(msg: "请输入密码");
                    return;
                  }
                  // Application().navigatePushToPage(context, Routes.homePage, {});
                  login(userName, password);
                }),
          ],
        ),
      ),
    );
  }

  login(String userName, String password) async {
    DioUtils.getInstance()?.post(ServiceApi.loginService, {
      // "userName": userName,
      // "password": password,
      'userName': userName,
      'password': password,
      // 'face': null,

    }, (successCallBack) {
      SpUtils.set("userType", successCallBack["data"]["userType"]);
      SpUtils.set("userId", successCallBack["data"]["userId"]);
      Navigator.pop(context);
      Application().navigatePushToPage(context, Routes.homePage, {});
    }, (errorCallBack) {
      ToastUtils.showToast(msg: "${errorCallBack["message"]}");
    });
  }
}
